Completed
Push — master ( fae22f...dbff67 )
by Leon
01:41
created

ents   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
1
jQuery( function( $ ) {
2
3
	/**
4
	 * JS meta_box field actions.
5
	 */
6
	var cmb2MetaBoxField = {
7
8
		/**
9
		 * Initialize meta box fields behavior.
10
		 */
11
		init: function() {
12
            this.showHideOnLoad();
13
			this.fieldTypeChange();
14
			this.fieldNameChange();
15
			this.displayUsageFunctions();
16
			this.bindActionsToEvents();
17
		},
18
19
		/**
20
		 * Show / hide field rows based on field_type_select selected value.
21
		 *
22
		 * showHideMain
23
		 *
24
		 *
25
		 *
26
		 */
27
		showHideMain: function( fieldTypeSelect ) {
28
            var fieldTypeVal = $( fieldTypeSelect ).val();
29
            var fieldSet     = $( fieldTypeSelect ).closest( '.cmb-field-list' );
30
            $( '.cmb-row.cmb_hide_field', fieldSet ).hide();
31
            $( '.cmb-row.' + fieldTypeVal, fieldSet ).show();
32
		},
33
34
		/**
35
		 * Show / hide field rows based on field_type_select selected value.
36
		 *
37
		 * showHideMain
38
		 *
39
		 *
40
		 *
41
		 */
42
		showHideSide: function() {
43
            var isRepeatable = $( '#_cmb2_repeatable_group' ).is( ':checked' );
44
			var repeatableOptions = $( '.cmb2-id--cmb2-group-description, .cmb2-id--cmb2-entry-name' );
45
			repeatableOptions.hide();
46
			if ( isRepeatable ) {
47
				repeatableOptions.show();
48
			}
49
		},
50
51
		/**
52
		 * Use the showHideMain() function on each field group's field rows on page load.
53
		 *
54
		 * showHideMainOnLoad
55
		 *
56
		 *
57
		 *
58
		 */
59
		showHideOnLoad: function() {
60
            var fieldTypeSelects = $( '.field_type_select' );
61
            fieldTypeSelects.each( function() {
62
                cmb2MetaBoxField.showHideMain( this );
63
            } );
64
			cmb2MetaBoxField.showHideSide();
65
66
		},
67
68
		/**
69
		 * Use the showHideMain() function on this field group's field rows on field_type_select change.
70
		 *
71
		 * fieldTypeChange
72
		 *
73
		 *
74
		 *
75
		 */
76
		fieldTypeChange: function() {
77
            $( '.cmb2-metabox' ).on( 'change', '.field_type_select', function( e ) {
78
                var fieldTypeSelect = e.target;
79
                cmb2MetaBoxField.showHideMain( fieldTypeSelect );
80
            } );
81
		},
82
83
		/**
84
		 * Use the showHideMain() function on this field group's field rows on field_type_select change.
85
		 *
86
		 * fieldTypeChange
87
		 *
88
		 *
89
		 *
90
		 */
91
		isRepeatableChanged: function() {
92
            $( '#_cmb2_repeatable_group' ).on( 'change', function( e ) {
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
93
                cmb2MetaBoxField.showHideSide();
94
            } );
95
		},
96
97
		/**
98
		 * Update the usage code snippets when field_name changes.
99
		 *
100
		 * fieldNameChange
101
		 *
102
		 *
103
		 *
104
		 */
105
		fieldNameChange: function() {
106
            $( '.cmb2-metabox' ).on( 'change keyup', '.field_name', function() {
107
                this.displayUsageFunctions();
108
            }.bind( this ) );
109
		},
110
111
		/**
112
		 * Convert field name to usage code snippet.
113
		 *
114
		 * displayUsageFunctions
115
		 *
116
		 *
117
		 *
118
		 */
119
		displayUsageFunctions: function() {
120
			var fieldName = $( '.field_name' );
121
			var wrapper = fieldName.closest( '.cmb-field-list' );
122
			var getPostMeta = $( '.get_post_meta', wrapper );
123
			var cmbf = $( '.cmbf', wrapper );
124
			var fieldNameVal = fieldName.val().toLowerCase().replace( / /g, '_' );
125
			getPostMeta.val( 'get_post_meta( get_the_ID(), \'_' + fieldNameVal + '\', true );' );
126
			cmbf.val( 'cmbf( get_the_ID(), \'_' + fieldNameVal + '\' );' );
127
		},
128
129
		/**
130
		 * Run the show / hide functions after reaptabel groups have been manipulated.
131
		 *
132
		 * bindActionsToEvents
133
		 *
134
		 *
135
		 *
136
		 */
137
		bindActionsToEvents: function() {
138
139
			$( document ).on( 'click', '.cmb-add-group-row, .cmb-remove-group-row, .cmb-shift-rows', function() {
140
				cmb2MetaBoxField.showHideOnLoad();
141
			} );
142
			$( '#_cmb2_repeatable_group' ).on( 'change', function() {
143
				cmb2MetaBoxField.showHideSide();
144
			} );
145
		}
146
	};
147
148
	cmb2MetaBoxField.init();
149
150
} );
151